package org.jeecg.modules.inspection.util;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.ribbon.proxy.annotation.Http;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import org.jeecg.common.constant.ErrorMsg;
import org.jeecg.common.exception.JeecgBootException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
@Component
@Slf4j
public class RestUtil {
        @Autowired
        private ObjectMapper objectMapper;

        @Autowired
        private RestTemplate restTemplate;

        public JSONObject postForEntity(JSONArray jsonArray, String url, String token, Map<String,Object> uriVariables) {
            if(uriVariables == null){
                uriVariables = new HashMap<>();
            }
            HttpHeaders headers = new HttpHeaders();

            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            if (token != null){
            headers.set("Authorization","bearer"+" "+token);
            }
            String value = null;
            try {
                value = jsonArray.toString();
            } catch (Exception e) {
                e.printStackTrace();
            }
            log.info("postForEntity接口请求参数="+value);
            HttpEntity<String> request = new HttpEntity<>(value, headers);
            ResponseEntity<String> result =  restTemplate.postForEntity(url,request,String.class,uriVariables);
            if(result.getBody() == null){
                throw new JeecgBootException(ErrorMsg.RETUAN_DATA_IS_NULL);
            }
                //String bodyString = new String(result.getBody().getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
            String bodyString = result.getBody();
            JSONObject returnJSON = JSONObject.parseObject(bodyString);
            if(returnJSON == null){
                throw new JeecgBootException(ErrorMsg.RETUAN_DATA_IS_NULL);
            }
            log.info("postForEntity接口返回数据="+bodyString);
            return returnJSON;
        }
    public JSONObject postForEntity(Map<String,Object> map, String url, Map<String,Object> uriVariables) {
        if(uriVariables == null){
            uriVariables = new HashMap<>();
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        String value = null;
        try {
            value = objectMapper.writeValueAsString(null);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        log.info("postForEntity接口请求参数="+value+url);
        HttpEntity<String> request = new HttpEntity<>(null, headers);
        ResponseEntity<String> result =  restTemplate.postForEntity(url,request, String.class,uriVariables);
        if(result.getBody() == null){
            throw new JeecgBootException(ErrorMsg.RETUAN_DATA_IS_NULL);
        }
        //String bodyString = new String(result.getBody().getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        String bodyString = result.getBody();
        JSONObject returnJSON = JSONObject.parseObject(bodyString);
        if(returnJSON == null){
            throw new JeecgBootException(ErrorMsg.RETUAN_DATA_IS_NULL);
        }
        log.info("postForEntity接口返回数据="+bodyString);
        return returnJSON;
    }
}

